package com.stx.friends.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.friends.dao.CommentDao;

import com.stx.friends.entity.CommentEntity;
import com.stx.friends.entity.StudentEntity;
import com.stx.friends.vo.CommentVo;

import com.sun.net.httpserver.Authenticator.Success;

public class CommentDaoImpl extends JdbcDaoSupport implements  CommentDao {

	@Override
	public List<CommentEntity> list( CommentVo vo) {

		String sql = "select * from WRX_COMMENT";
		JdbcTemplate jt = getJdbcTemplate();
		List< CommentEntity> stuList = jt.query(sql, new CommentRowMapper());
		return stuList;
	}

	class  CommentRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			 CommentEntity entity = new CommentEntity();

			entity.setwrxUid(rs.getString("wrxUid"));
			entity.setWrxId(rs.getString("wrxId"));
			entity.setWrxContent(rs.getString("wrxContent"));
			entity.setWrxUid(rs.getString("wrxUid"));
			entity.setCreateTime(rs.getString("createTime"));
			entity.setDelFlag(rs.getString("delFlag"));
			entity.setReadFlag(rs.getString("readFlag"));

			return entity;
		}
	}

	@Override
	public void delComment(String wrxUid) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from WRX_COMMENT where wrxid="+wrxUid;

		// 构建参数数组

		jt.execute(sql);

	}

	@Override
	public void addComment(CommentVo comment) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into WRX_COMMENT(wrxid,bid,wrxContent,wrxUid) values(seq_wrx_comment.nextval,?,?,?)";

		// 构建参数数组
		Object[] args = { comment.getWrxId(),
				comment.getWrxContent(),comment.getWrxUid(),
				};
		
		jt.update(sql, args);

	}

	@Override
	public CommentEntity getCommentById(String wrxUid) {
		String sql = "select * from WRX_COMMENT where wrxid="+wrxUid;

		JdbcTemplate jt = getJdbcTemplate();

		 CommentEntity entity = (CommentEntity) jt.query(sql, new CommentRowMapper()).get(0);
		return entity;
	}

	@Override
	public void updateComment(CommentVo comment) {
		String sql="update WRX_COMMENT set WRXCONTENT=? where wrxid= ?";
		JdbcTemplate jt=getJdbcTemplate();
		System.out.println(comment.getWrxContent()+comment.getwrxUid());
		Object[] args={comment.getWrxContent(),comment.getWrxId()};
		jt.update(sql, args);
		
		
	}
}
